package com.woniuxy.his.controller;


import com.woniuxy.his.common.Result;
import com.woniuxy.his.exception.GlobalExceptionHandle;
import com.woniuxy.his.exception.ParamExceptionHandle;
import com.woniuxy.his.service.ICaseService;
import com.woniuxy.his.vo.InspectVo;
import com.woniuxy.his.vo.OutMedicineDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 病历表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@RestController
@CrossOrigin("*")
@Slf4j
public class CaseController {

    @Resource
    private ICaseService caseService;

    //展示为出药清单
    @GetMapping("showMediciningList")
    public Result showMediciningList(Integer prescriptionId, String startTime, String stopTime) throws ParseException {
        log.info("病人id:{},开始时间:{}，结束时间:{}",prescriptionId,startTime,stopTime);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = sdf.parse(startTime);
        Date time2 = sdf.parse(stopTime);
        log.info("转换后开始时间{}转换后结束时间{}",time1,time2);
        try {
            List<OutMedicineDetailVo> medicines = caseService.getAllMedicineByPrescriptionId(prescriptionId, time1, time2);
            return Result.success("20000", "查询成功", medicines);
        } catch (ParamExceptionHandle e){
            log.warn("当日无病历信息{}",e.getMessage());
            return Result.fail("50000","参数异常",null);
        }catch (Exception e){
            log.error("系统异常{}",e);
            return Result.fail("40005","系统异常",null);
        }
    }

    //展示已出药清单
    @GetMapping("showMedicinedList")
    public Result showMedicinedList(Integer prescriptionId, String startTime, String stopTime) throws ParseException {
        log.info("病人id:{},开始时间:{}，结束时间:{}",prescriptionId,startTime,stopTime);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = sdf.parse(startTime);
        Date time2 = sdf.parse(stopTime);
        log.info("转换后开始时间{}转换后结束时间{}",time1,time2);
        try {
            List<OutMedicineDetailVo> medicines = caseService.getAllMedicineByPrescriptionId(prescriptionId, time1, time2);
            return Result.success("20000", "查询成功", medicines);
        } catch (ParamExceptionHandle e){
            log.warn("当日无病历信息{}",e.getMessage());
            return Result.fail("50000","参数异常",null);
        }catch (Exception e){
            log.error("系统异常{}",e);
            return Result.fail("40005","系统异常",null);
        }
    }


    /**
     * 根据患者名称获得患者检查项目
     * @param patientName
     * @return
     */
    @RequiresRoles("checker")
    public Result findInspectByPatientName(String patientName){
        log.info("根据患者名称获得患者检查项目前端传入患者名称：{}",patientName);

        List<InspectVo> inspectVoDBs = caseService.findInspectByPatientName(patientName);
        log.info("根据患者名称获得患者检查项目数据库查询结果：{}",inspectVoDBs);


        return Result.success("20000","成功",inspectVoDBs);
    }
}

